﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Housing;

public partial class HouseKeep_Search : System.Web.UI.Page
{
    Apply apply = new Apply();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserStreet"] == null)
            WebHelper.AlertAndRedirect("登陆超时，请重新登陆","../Login.aspx");
        if (!IsPostBack)
        {
            BindGridview();
            PageBind();
            ddlpages.Items.Clear();
            for (int i = 1; i <= gvshowbysearch.PageCount; i++)
            {
                ddlpages.Items.Add("第" + i + "页");
            }
        }
    }
    protected void BindGridview()
    {
        if (string.IsNullOrEmpty(txbcontent.Text))
        {
            DataSet ds = apply.SearchAllApply(Session["UserStreet"].ToString());
            gvshowbysearch.DataSource = ds;
            gvshowbysearch.DataBind();
        }
        else
        {
            if (ddlsearch.SelectedValue == "编号")
            {
                DataSet ds = apply.SearchApplyByTerms("apply_longid", txbcontent.Text, Session["UserStreet"].ToString());
                gvshowbysearch.DataSource = ds;
                gvshowbysearch.DataBind();
                PageBind();
            }
            else
            {
                DataSet ds = apply.SearchApplyByTerms("apply_name", txbcontent.Text, Session["UserStreet"].ToString());
                gvshowbysearch.DataSource = ds;
                gvshowbysearch.DataBind();
                PageBind();
            }
        }
    }
    protected void PageBind()
    {
        //DataSet ds = apply.SearchAllApply(Session["UserStreet"].ToString());
        //gvshowbysearch.DataSource = ds;
        //gvshowbysearch.DataBind();
        if (gvshowbysearch.PageIndex == 0 && gvshowbysearch.PageCount == 1)
        {
            btnfirstpage.Enabled = false;
            btnlastpage.Enabled = false;
            btnnextpage.Enabled = false;
            btnprepage.Enabled = false;
        }
        else if (gvshowbysearch.PageIndex == 0 && gvshowbysearch.PageCount > 1)
        {
            btnfirstpage.Enabled = false;
            btnlastpage.Enabled = true;
            btnnextpage.Enabled = true;
            btnprepage.Enabled = false;
        }
        else if (gvshowbysearch.PageIndex > 0 && gvshowbysearch.PageIndex < gvshowbysearch.PageCount - 1)
        {
            btnfirstpage.Enabled = true;
            btnlastpage.Enabled = true;
            btnnextpage.Enabled = true;
            btnprepage.Enabled = true;
        }
        else if (gvshowbysearch.PageIndex == gvshowbysearch.PageCount - 1)
        {
            btnfirstpage.Enabled = true;
            btnlastpage.Enabled = false;
            btnnextpage.Enabled = false;
            btnprepage.Enabled = true;
        }
    }
    public string GetSituation(string state, string passed)
    {
        if (passed == "3")
            return "审核通过";
        else
        {
            if (state == "5")
                return "待管理中心审核";
            else if (state == "2")
                return "待街道初审";
            else if (state == "3")
                return "待辖区房管所审核";
            else if (state == "4")
                return "待民政部门审核";
            else
                return "待街道公布";
        }
    }
    protected void btnsearch_Click(object sender, EventArgs e)
    {
        BindGridview();
        ddlpages.Items.Clear();
        for (int i = 1; i <= gvshowbysearch.PageCount; i++)
        {
            ddlpages.Items.Add("第" + i + "页");
        }
    }
    protected void btnnextpage_Click(object sender, EventArgs e)
    {
        int index = gvshowbysearch.PageIndex;
        gvshowbysearch.PageIndex = index + 1;
        BindGridview();
        PageBind();
    }
    protected void btnprepage_Click(object sender, EventArgs e)
    {
        int index = gvshowbysearch.PageIndex;
        if (index >= 1)
        {
            gvshowbysearch.PageIndex = index - 1;
        }
        else {
            gvshowbysearch.PageIndex = 0;
        }
        BindGridview();
        PageBind();
    }
    protected void btnfirstpage_Click(object sender, EventArgs e)
    {
        gvshowbysearch.PageIndex = 0;
        BindGridview();
        PageBind();
    }
    protected void btnlastpage_Click(object sender, EventArgs e)
    {
        gvshowbysearch.PageIndex = gvshowbysearch.PageCount - 1;
        BindGridview();
        PageBind();
    }
    protected void ddlpages_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index=0;
        if(ddlpages.SelectedItem.ToString().Substring(2, 1)=="页")
            index = Convert.ToInt32(ddlpages.SelectedItem.ToString().Substring(1, 1));
        else
            index = Convert.ToInt32(ddlpages.SelectedItem.ToString().Substring(1, 2));
        gvshowbysearch.PageIndex = index - 1;
        BindGridview();
        PageBind();
    }
    protected void gvshowbysearch_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //LinkButton lkbtndel = (LinkButton)e.Row.FindControl("lkbtndel");
        //if (lkbtndel != null)
        //    lkbtndel.Attributes.Add("onclick","return confirm('确定要删除？')");
    }
    protected void gvshowbysearch_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //if (e.CommandName.ToLower() == "del")
        //{
        //    //向房管中心提交删除申请
        //}
    }
}
